#!/usr/bin/env python
# -*- coding: UTF-8 -*-
'''
@Project ：self-practice 
@File    ：producer.py
@IDE     ：PyCharm 
@Author  ：w50011605
@Email   ：wangqiyu1@xfusion.com
@Date    ：2025/6/25 10:21 
'''

import pika
from RabbitMQConnection import RabbitMQConnection
from pika.channel import Channel

print(pika.__file__)

def select_communication_mode(mode: str, rmq: RabbitMQConnection):
    """根据输入选择通信模式"""
    if mode == "base":
        base_queue_communication(rmq)
    elif mode == "topic":
        topic_communication(rmq)
    else:
        print("Invalid communication mode selected.")

def base_queue_communication(rmq: RabbitMQConnection):
    """基本的队列通信示例"""
    channel = rmq.get_channel()
    channel.queue_declare(queue='test_queue')  # 声明队列
    channel.basic_publish(exchange='', routing_key="test_queue", body='Hello World')
    print("[x] Sent 'Hello World'")



"""
    《消息持久化》
    channel.queue_declare(queue='task_queue', durable=True)  # 队列持久化
    channel.basic_publish(
        exchange='',
        routing_key='task_queue',
        body='This is a persistent task',
        properties=pika.BasicProperties(
            delivery_mode=2,  # 消息持久化
    )
)
"""
def topic_communication(rmq: RabbitMQConnection):
    """基于主题的交换机通信示例"""
    channel = rmq.get_channel()
    channel.exchange_declare(exchange='topic_logs', exchange_type='topic')

    routing_key = 'user.notification.email'
    message = 'Email sent to user!'
    channel.basic_publish(
        exchange="topic_logs",
        routing_key=routing_key,
        body=message
    )
    print(f"[x] Sent {routing_key}:{message}")


# 使用类来管理RabbitMQ连接
rmq = RabbitMQConnection(host='localhost')

# 选择通信模式
mode = input("Enter communication mode ('base' or 'topic'): ").lower()

select_communication_mode(mode, rmq)

# 关闭连接
rmq.close()